package com.wang.controller;


import com.sun.istack.internal.NotNull;
import com.wang.pojo.Employee;
import com.wang.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author admin
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    EmployeeService employeeService;

    @GetMapping("/user/get")
    public String getEmp(Model model) {
        List<Employee> emp = employeeService.getEmp();
        model.addAttribute("empInfo", emp);
        return "list";
    }

    @GetMapping("/user/add")
    public String toAdd() {
        return "add";
    }

    @PostMapping("/admin/add")
    public String add(Employee employee) {
        employeeService.saveEmp(employee);
        return "redirect:/emp/user/get";
    }

    @GetMapping("/admin/delete/{id}")
    public String delete(@PathVariable Integer id) {
        employeeService.deleteEmp(id);
        return "redirect:/emp/user/get";
    }

    @GetMapping("/admin/toUpdate/{id}")
    public String toUpdate(@PathVariable int id, @NotNull Model model) {
        Employee info = employeeService.getEmpById(id);
        model.addAttribute("msg", info);
        return "update";
    }

    @RequestMapping("/admin/updateEmp")
    public String update(Employee employee) {
        employeeService.updEmpById(employee);
        return "redirect:/emp/user/get";
    }
}
